import Vue from 'vue'
import App from './App.vue'
import router from './routes'
import store from './store';
import ElementUI from 'element-ui';
import VueI18n from 'vue-i18n';
import enLocale from 'element-ui/lib/locale/lang/en';
import zhLocale from 'element-ui/lib/locale/lang/zh-CN';
import filters from './libs/filters';
import Api from './api';
import ExportExcel from './api/export-excel';
import './assets/scss/element-variables.scss';
import './assets/scss/index.scss';
import urlUtil from './libs/urlutils';
import { apiConf } from './config';
import urlType from './api/url-type';
import storage from './libs/storage';
import localeZhCN from './locale/zh-CN';
import localeEn from './locale/en';

// import HidePhoneNum from './components/hide-phone-num/index';
import VueClipboard from 'vue-clipboard2'
import urlUtils from './libs/urlutils';

Vue.config.productionTip = false;

// 禁用拖拽打开新标签页
document.body.ondrop = function (event) {
    event.preventDefault();
    event.stopPropagation();
};

//复制
Vue.use(VueClipboard)
// 国际化
Vue.use(VueI18n);
// 默认语言
const DEFAULT_LANGUAGE = 'en';
Vue.config.lang = DEFAULT_LANGUAGE;
zhLocale.ql = localeZhCN;
enLocale.ql = localeEn;
const messages = {
    zhCN: {
        ...zhLocale,
        ...localeZhCN
    },
    en: {
        ...enLocale,
        ...localeEn
    }
};

const i18n = new VueI18n({
    locale: DEFAULT_LANGUAGE,
    messages
});

// 引入 Element
Vue.use(ElementUI, {
    i18n: (key, value) => i18n.t(key, value)
});

/**
 * 显示1秒带有关闭按钮的成功提示框
 * @param msg
 */
Vue.prototype.$message.successOneSecond = (msg = '') => {
    if (msg && typeof msg === 'string') {
        Vue.prototype.$message({
            message: msg,
            type: 'success',
            showClose: true,
            duration: 1000,
        });
    }
};

/**
 * 显示1秒带有关闭按钮的错误提示框
 * @param msg
 */
Vue.prototype.$message.errorOneSecond = (msg = '') => {
    if (msg && typeof msg === 'string') {
        Vue.prototype.$message({
            message: msg,
            type: 'error',
            showClose: true,
            duration: 1000,
        });
    }
};

// 引入隐藏手机号组件
// Vue.use(HidePhoneNum);

// 全局挂载 filter
// eslint-disable-next-line
Object.keys(filters).forEach((key, index) => {
    Vue.filter(key, filters[key]);
});

/**
 * 打开新的标签页
 */
const openNewBlank = (url) => {
    console.warn(url,'openNewBlank')
    const loc = window.location;
    const prefix = loc.protocol + '//' + loc.host + loc.pathname;
    if (url && url.indexOf('//') === -1) {
        url = prefix + url;
    }
    window.open(url);
};

Vue.prototype.$openNewBlank = openNewBlank;

Vue.prototype.$urlUtil = urlUtil;
Vue.prototype.$apiConf = {
    baseUrl: apiConf.baseUrl,
    urlType,
    urlUtils,
};
Vue.prototype.$storage = storage;
// 挂载 Api, 在组件内可直接 this.$api 访问
Vue.use(Api);
Vue.use(ExportExcel);

new Vue({
    router,
    store,
    i18n,
    render: h => h(App),
    created() {
        const user = storage.get('user');
        if (user) {
            store.dispatch('getUserAuthority');
        }
        const lang = storage.getLocal('language');
        if (lang) {
            const locale = lang === 'zh' ? 'zhCN' : 'en';
            Vue.config.lang = locale;
            i18n.locale = locale;
            storage.setLocal('language', locale === 'zhCN' ? 'zh' : 'en');
            this.local = locale;
        } else {
            storage.setLocal('language', DEFAULT_LANGUAGE === 'zhCN' ? 'zh' : 'en');
            this.local = 'en';
        };
    }
}).$mount('#app');
